﻿using System.Collections.Generic;
using System.Linq;

namespace NC.Data
{
    public class PostRepository
    {
        private readonly NEPALICORNEREntities _ncEntities;

        public PostRepository()
        {
            _ncEntities = new NEPALICORNEREntities();
        }

        public Post SaveNewPost(Post dataPost)
        {
            _ncEntities.Posts.Add(dataPost);
            _ncEntities.SaveChanges();
            return dataPost;
        }

        public IList<Category> GetAllPostCategories()
        {
            return _ncEntities.Categories.ToList();
        }

        public IList<Address> GetAllLocations()
        {
            return _ncEntities.Addresses.ToList();
        }

        public IList<Post> GetAllPost()
        {
            return _ncEntities.Posts.ToList();
        }

        public IList<Post> GetAllAvailablePosts()
        {
            return _ncEntities.Posts.Where(x=> !x.Needed).ToList();
        }
        public IList<Post> GetAllNeededPosts()
        {
            return _ncEntities.Posts.Where(x => x.Needed).ToList();
        }
        public Post GetPostById(int id)
        {
            return _ncEntities.Posts.SingleOrDefault(x=>x.Id==id);
        }

        public IList<Post> GetNeededPostsByCategory(int categoryId)
        {
            return _ncEntities.Posts.Where(x => x.Needed && x.CategoryId == categoryId).ToList();
        }

        public IList<Post> GetAvailablePostsByCategory(int categoryId)
        {
            return _ncEntities.Posts.Where(x => !x.Needed && x.CategoryId == categoryId).ToList();
        }
    }
}